<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()">立即注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    data() {
      var password = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('密码不能为空'));
        } else {
          if (value.length < 6) {
            return callback(new Error('密码长度不能小于6'));
          }
        }

      };
      return {
        labelPosition: 'right',
        ruleForm: {
          username: '',
          password: '',
        },
        rules: {
          username: [
            {required: true, message: '请输入名称'},
            {min: 5, message: '长度6个字符以上', trigger: 'blur'}
          ],
          password: [
            {validator: password, trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      submitForm() {
        this.axios.post('http://localhost:8000/regirst/', this.qs.stringify({
          username: this.ruleForm.username, password: this.ruleForm.password
        })).then(res => {
          if (res.data.code == 200) {
            this.$router.push({name: 'login'})
          } else {
            this.$message('注册失败')
          }
        })
      },
    }
  }
</script>

<style scoped>

</style>
